package com.example.demoredis.controller;

import com.example.democommon.tools.Result;
import com.example.demoredis.tools.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.Set;

/**
 * <p>
 * 用户控制器
 * </p>
 *
 * @author ross zhang
 * @version 1.0
 * @PackageName demo-one
 * @Package com.example.demoredis.controller
 * @since 2022/4/16 14:06
 */
@RestController
public class UserController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取用户当前在线人数
     * @return
     */
    @GetMapping("/getOnlineCount")
    public Result<?> getOnlineCount(@RequestParam(name = "key")String key) {
        //获得所有的key
        Set<String> keys = redisUtil.getCount(key);
//        Set<String> keys = redisUtil.getCount("sys:user:" + "*");
        if (keys.size() > 0) {
            return Result.OK("成功！","当前在线人数："+keys.size());
        } else {
            return Result.OK("成功！","当前在线人数："+0);
        }
    }

}
